package com.bci.pwtz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.GeoCodeMapper;
import com.bci.pwtz.mysql.model.GeoCode;
import com.bci.pwtz.service.GeoCodeService;
@Service
public class GeoCodeServiceImpl implements GeoCodeService
{
    private GeoCodeMapper geoCodeMapper;
    public GeoCodeMapper getGeoCodeMapper()
    {
        return geoCodeMapper;
    }
    public void setGeoCodeMapper(GeoCodeMapper geoCodeMapper)
    {
        this.geoCodeMapper = geoCodeMapper;
    }
    private static final Logger _logger=Logger.getLogger(GeoCodeServiceImpl.class);
    
    @Override
    public List<GeoCode> findGeoCodeList(Long parent) throws PwtzDatabaseException
    {
        List<GeoCode> list=null;
        if(_logger.isInfoEnabled()){
            _logger.info("findGeoCodeList:|parent="+parent.toString());
        }
        if(parent==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("findGeoCodeList,parent is null");
            }
        }
        try{
            list=geoCodeMapper.selectGeoCodes(parent);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if(list!=null){
            _logger.debug("findGeoCodeList:result list="+list.toString());
        }
        return list;
    }
    
    @Override
    public Map<String, String> findGeoByLevel(String grade) throws PwtzDatabaseException
    {
        Map<String, String> geoMap = new HashMap<String, String>();
        List<GeoCode> list=null;
        if(_logger.isInfoEnabled()){
            _logger.info("findGeoByLevel:|grade="+grade);
        }
        if(grade==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("grade,grade is null");
            }
        }
        try{
            list=geoCodeMapper.selectGeoCodesByLevel(grade);
            if(list!=null && list.size()>0){
                for(GeoCode geo:list){
                    geoMap.put(geo.getGbCode().toString(), geo.getGbName());
                }
            }
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
//        if(list!=null){
//            _logger.debug("findGeoByLevel:result list="+list.toString());
//        }
        return geoMap;
    }

}
